package com.acp.controller;

import com.acp.dao.acpo.DcsTaskPerformanceMapper;
import com.acp.model.DcsTaskPerformance;
import com.acp.param.DcsTaskParam;
import com.acp.result.PageResult;
import com.acp.result.ResultInfo;
import com.acp.service.DcsTaskPerfoemanceService;
import com.acp.service.DcsTaskService;
import com.acp.util.BeanValidator;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.List;
/**
 * All rights Reserved, Designed By 姑苏区政府大数据中心
 *
 * @version V1.0
 * @Title: null.java
 * @Package com.dci.controller.construction
 * @Description: 任务管理
 * @author: 张星亮
 * @date: 2019/4/5
 * @Copyright: 2019 姑苏区政府大数据中心. All rights reserved.
 * 注意：本内容仅限于姑苏区政府大数据中心内部传阅，禁止外泄以及用于其他的商业目
 */

@Controller
@RequestMapping("/task")
public class DcsTaskController {

    @Resource
    private DcsTaskService dcsTaskService;

    @Resource
    private DcsTaskPerfoemanceService dcsTaskPerfoemanceService;

    @Value("${upload.path}")
    private String path;

    @Value("${upload.urlIP}")
    private String urlIP;

    /**
     * 新增
     * @param form
     * @return ResultInfo 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @PostMapping("/addTask")
    @ResponseBody
    public ResultInfo addTask( @RequestParam("form") String form, @RequestParam("file") MultipartFile[] files) {

        JSONObject jsonObject = JSONObject.parseObject( form );
        DcsTaskParam dcsTaskParam = JSONObject.toJavaObject( jsonObject, DcsTaskParam.class );

        String address = dcsTaskParam.getTask_code();
        String filePath = path + File.separator + address + "/";
        String fileNames = "";
        if (files.length != 0) {
            for (MultipartFile file : files) {
                String fileName = file.getOriginalFilename();
                if (fileNames == "") {
                    fileNames += fileName;
                } else {
                    fileNames += ',' + fileName;
                }
                File fileDir = new File(filePath);
                if (!fileDir.exists()) {
                    fileDir.mkdirs();
                }
                try {
                    file.transferTo(new File(fileDir.getAbsolutePath(), fileName));
                } catch (IOException e) {
                    continue;
                }
            }
        }
        // 数据库存储的相对路径
        String urlPath = urlIP + address + "/";

        BeanValidator.check(dcsTaskParam);
        return dcsTaskService.addDcsTask(dcsTaskParam,urlPath,fileNames);
    }

    /**
     * 查询所有信息
     * @param list
     * @return PageResult 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @PostMapping("/queryTask")
    @ResponseBody
    public PageResult queryTaskList(@RequestBody List<DcsTaskParam> list, String name) {
        PageResult pageResult = dcsTaskService.queryDcsTaskList(list, name);
        return pageResult;
    }

    /**
     * 查询所有Remind信息
     * @param name
     * @return PageResult 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @PostMapping("/queryTaskRemind")
    @ResponseBody
    public PageResult queryTaskRemindList(@RequestBody(required = false) String name) {
        PageResult pageResult = dcsTaskService.queryDcsTaskRemindList(name);
        return pageResult;
    }

    /**
     * 根据主键删除
     * @param list
     * @return ResultInfo 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @PostMapping("/deleteTask")
    @ResponseBody
    public ResultInfo deleteGisElementById(@RequestBody List<DcsTaskParam> list) {
        ResultInfo resultInfo = dcsTaskService.deleteDcsTaskById(list);
        return resultInfo;
    }

    /**
     * 编辑
     * @return ResultInfo 封装的返回参数  保存成功msg:906 保存失败msg:911
     * @throws Exception
     */
    @PostMapping("/editTask")
    @ResponseBody
    public ResultInfo editGisElement(@RequestParam("form") String form, @RequestParam("file") MultipartFile[] files) {
        JSONObject jsonObject = JSONObject.parseObject( form );
        DcsTaskParam dcsTaskParam = JSONObject.toJavaObject( jsonObject, DcsTaskParam.class );

        String address = dcsTaskParam.getTask_code();
        String filePath = path + File.separator + address + "/";
        String fileNames = "";
        String name = "";
        if (files.length != 0) {
            for (MultipartFile file : files) {
                String fileName = file.getOriginalFilename();
                if (fileNames == "") {
                    fileNames += fileName;
                } else {
                    fileNames += ',' + fileName;
                }
                File fileDir = new File(filePath);
                if (!fileDir.exists()) {
                    fileDir.mkdirs();
                }
                try {
                    file.transferTo(new File(fileDir.getAbsolutePath(), fileName));
                } catch (IOException e) {
                    continue;
                }
            }
        }
        // 数据库存储的相对路径
        String urlPath = urlIP + address + "/";
        if ("".equals(fileNames)) {
            name = dcsTaskParam.getPhotoName();
        }else if ("".equals(dcsTaskParam.getPhotoName())){
            name = fileNames;
        }else {
            name = dcsTaskParam.getPhotoName() + "," + fileNames;
        }

        BeanValidator.check(dcsTaskParam);
        return dcsTaskService.editDcsTask(dcsTaskParam,urlPath,name);
    }

    /**
     * 根据主键删除反馈信息
     * @param list
     * @return ResultInfo 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @PostMapping("/deleteFeed")
    @ResponseBody
    public ResultInfo deleteByFeed(@RequestBody List<DcsTaskParam> list) {
        ResultInfo resultInfo = dcsTaskService.deleteByfeedback(list);
        return resultInfo;
    }

    /**
     * 查询所有任务绩效内容
     * @param group_code
     * @return list集合
     */
    @PostMapping("/selectPerformance")
    @ResponseBody
    public List<DcsTaskPerformance> selectAll(@RequestBody(required = false) DcsTaskPerformance group_code) {
        String code = group_code.getGroup_code();
        List<DcsTaskPerformance> list = dcsTaskPerfoemanceService.selectAll(code);
        return list;
    }

    /**
     * 编辑
     * @param performance
     * @return ResultInfo 封装的返回参数  保存成功msg:906 保存失败msg:911
     */
    @PostMapping("/editScore")
    @ResponseBody
    public ResultInfo updateScore(@RequestBody(required = false) String performance) {
        return dcsTaskPerfoemanceService.updateScore(performance);
    }

}
